package com.example.viewmodeldemo;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.viewmodeldemo.databinding.ActivityTimerBinding;

/**
 * @author: tuyrt7 on 2020/9/3.
 */
public class TimerActivity extends AppCompatActivity {

    private ActivityTimerBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_timer);

        TimerViewModel viewModel = new ViewModelProvider(this).get(TimerViewModel.class);

//        viewModel.setOnTimerChangeListener(new TimerViewModel.OnTimerChangeListener() {
//            @Override
//            public void onchange(int second) {
//                runOnUiThread(() -> {
//                    Log.d("aaaa", "onchange: " + second);
//                });
//            }
//        });

        viewModel.getCurrentSecond().observe(this,second -> {
            mBinding.tvTime.setText("Time:" + second);
        });

        viewModel.startTiming();
        mBinding.btnReset.setOnClickListener(v -> {
            viewModel.reset();
        });
        viewModel.startTiming();

//        Observer<Integer> observer = integer -> {
//        };
//        viewModel.getCurrentSecond().observeForever(observer);
//        viewModel.getCurrentSecond().removeObserver(observer);



    }
}
